package lumis.service.wsrp.consumer.producermanager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.CollectionOfElements;

/**
 * Cloned portlet entity.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
@Entity(name = "lumis.service.wsrp.consumer.producermanager.ClonedPortlet")
@Table(name = "lum_WsrpCClonedPortlet")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@SuppressWarnings("serial")
public class ClonedPortlet implements Serializable
{
	@Id	
	@Column(name = "portletHandle")
	private String portletHandle;
	
	@Column(name = "name")
	private String name;
	
	@ManyToOne()
	@JoinColumn(name = "registrationHandle")
	private Registration registration;
	
	@CollectionOfElements(fetch=FetchType.EAGER)
	@JoinTable(name="lum_WsrpCClonedTgtInst", joinColumns=@JoinColumn(name="portletHandle"))
	@Column(name="interfaceInstanceId", nullable=false)
	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	private Collection<String> interfaceInstanceIds = new ArrayList<String>();
	
	public ClonedPortlet()
	{
	}
	
	public ClonedPortlet(String portletHandle, String name)
	{
		this.portletHandle = portletHandle;
		this.name = name;
	}
	
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getPortletHandle()
	{
		return portletHandle;
	}

	void setPortletHandle(String portletHandle)
	{
		this.portletHandle = portletHandle;
	}
	
	/**
	 * Returns the interface instances the cloned portlet is being used.
	 * @return collection of interface instance ids that are using the cloned portlet.
	 */
	public Collection<String> getInterfaceInstanceIds()
	{
		return interfaceInstanceIds;
	}

	/**
	 * Sets the interface instance ids that are using the cloned portlet.
	 * @param interfaceInstanceIds collection of ids of interface instances that use the cloned portlet.
	 */
	public void setInterfaceInstanceIds(Collection<String> interfaceInstanceIds)
	{
		this.interfaceInstanceIds = interfaceInstanceIds;
	}
	
	/**
	 * Adds an interface instance id to the list of interface instances that use the cloned portlet.
	 * @param interfaceInstanceId id of interface instances that use the cloned portlet.
	 */
	public void addInterfaceInstanceId(String interfaceInstanceId)
	{
		if (!this.interfaceInstanceIds.contains(interfaceInstanceId))
		{
			this.interfaceInstanceIds.add(interfaceInstanceId);	
		}
	}

	public String getRegistrationHandle()
	{
		return registration.getRegistrationHandle();
	}

	public Registration getRegistration()
	{
		return registration;
	}
	
	public void setRegistration(Registration registration)
	{
		this.registration = registration;
	}
}